package com.tenement.service.impl;

import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import com.tenement.dao.TenementHouseClientMapper;
import com.tenement.dao.TenementLogMapper;
import com.tenement.dao.TenementSectorMapper;
import com.tenement.dao.TenementUserMapper;
import com.tenement.domain.TenementHouseClient;
import com.tenement.domain.TenementLog;
import com.tenement.domain.TenementSector;
import com.tenement.domain.TenementUser;
import com.tenement.service.HouseOrClientService;
import com.tenement.utils.HoseOrClientVo;
import com.tenement.utils.Tools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @ClassName : HouseOrClientServiceImpl  //类名
 * @Description : 通知服务接口  //描述
 * @Author : 易嵘  //作者
 * @Date: 2020-06-10 21:21  //时间
 */
@Service
public class HouseOrClientServiceImpl implements HouseOrClientService {

    @Resource
    TenementHouseClientMapper houseClientMapper;

    @Resource
    TenementUserMapper userMapper;

    @Resource
    TenementSectorMapper sectorMapper;

    @Resource
    TenementLogMapper tenementLogMapper;

    @Override
    public List<TenementHouseClient> findAll(TenementHouseClient houseClient, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        return houseClientMapper.findAll(houseClient);
    }

    @Override
    public Long overCount(TenementHouseClient houseClient) {
        return houseClientMapper.overCount(houseClient);
    }

    @Override
    public int deleteById(String id, TenementUser user, String ip) {
        sectorMapper.deleteById(id);
        TenementLog log=new TenementLog();
        log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
        log.setLogType(2);
        StringBuffer logBody= new StringBuffer();
        Tools.logHouseOrClientMod(houseClientMapper.selectByPrimaryKey(id),logBody);
        log.setLogBody(logBody.toString());
        log.setLogTitle("删除通知信息");
        log.setLogIp(ip);
        log.setCreatedBy(user.getUserName());
        tenementLogMapper.insertSelective(log);
        return houseClientMapper.deleteById(id);
    }

    @Override
    public int deleteAll(String[] ids, TenementUser user, String ip) {
        if (ids != null) {
            List<String> list = new ArrayList<>(Arrays.asList(ids));
            TenementLog log=new TenementLog();
            log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
            log.setLogType(2);
            StringBuffer logBody= new StringBuffer();
            Tools.logHouseOrClientMods(houseClientMapper.findAllId(list),logBody);
            log.setLogBody(logBody.toString());
            log.setLogTitle("批量删除通知信息");
            log.setLogIp(ip);
            log.setCreatedBy(user.getUserName());
            tenementLogMapper.insertSelective(log);
            sectorMapper.deleteAll(list);
            return houseClientMapper.deleteAll(list);
        }
        return 0;
    }

    /**
     *  新增
     * @param houseClient 通知模型
     * @param user 当前用户
     * @param ip
     * @return
     */
    @Override
    public int add(TenementHouseClient houseClient, TenementUser user, String ip) {
        houseClient.setHouseClientId(UUID.randomUUID().toString().replaceAll("-", ""));
        houseClient.setCreatedBy(user.getUserName());
        //houseClient.setCreatedTime(new Date());
        houseClient.setRevision(1);

        List<TenementUser> list = userMapper.findAll();
        List<TenementSector> sectors = new ArrayList<>();
        for (TenementUser tenementUser : list) {
            TenementSector sector = new TenementSector();
            sector.setSectorId(UUID.randomUUID().toString().replaceAll("-", ""));
            sector.setInformId(houseClient.getHouseClientId());
            sector.setSectorName(tenementUser.getUserId());
            sector.setRevision(0);
            sectors.add(sector);
        }
        //将通知绑定给每个用户
        sectorMapper.add(sectors);
        TenementLog log=new TenementLog();
        log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
        log.setLogType(1);
        StringBuffer logBody= new StringBuffer();
        Tools.logHouseOrClientMod(houseClient,logBody);
        log.setLogBody(logBody.toString());
        log.setLogTitle("新增通知信息");
        log.setLogIp(ip);
        log.setCreatedBy(user.getUserName());
        tenementLogMapper.insertSelective(log);
        return houseClientMapper.insertSelective(houseClient);
    }

    @Override
    public TenementHouseClient findById(String houseClientId) {
        return houseClientMapper.selectByPrimaryKey(houseClientId);
    }

    @Override
    public List<HoseOrClientVo> selectAll(TenementUser user) {
        List<HoseOrClientVo> hoseOrClientVos=new ArrayList<>();
        List<TenementHouseClient> list=houseClientMapper.selectAll();
        for (TenementHouseClient houseClient : list) {
            HoseOrClientVo hoseOrClientVo=new HoseOrClientVo();
            TenementSector sector=sectorMapper.selectOne(houseClient.getHouseClientId(),user.getUserId());
            if(sector!=null){
                hoseOrClientVo.setRevision(sector.getRevision());
                hoseOrClientVo.setName(houseClient.getClientId());
                hoseOrClientVo.setValue(houseClient.getHouseClientId());
                hoseOrClientVo.setDates(houseClient.getCreatedTime());
                hoseOrClientVos.add(hoseOrClientVo);
            }

        }
        return hoseOrClientVos;
    }

    @Override
    public int showOne(String houseClientId, String userId) {
        return sectorMapper.showOne(houseClientId,userId);
    }

    @Override
    public TenementHouseClient selectLatest() {
        return houseClientMapper.selectLatest();
    }

}
